<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="callRoomList">
      <el-table-column label="话单编号" align="center" prop="ucid"/>
      <el-table-column label="房间状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.call_room_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="开始时间" align="center" prop="createTime" width="180">

      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="endTime" width="180">

      </el-table-column>

      <el-table-column label="房间录音" align="center" prop="recordTask">
        <template slot-scope="scope">
          <span v-if="!scope.row.recordUrl">-</span>
          <M3U8Preview :src="$resRecordBaseUrl+scope.row.recordUrl"></M3U8Preview>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />


  </div>
</template>

<script>
import {addCallRoom, delCallRoom, getCallRoom, listCallRoom, updateCallRoom} from "@/api/service/call/callRoom";
import Hls from 'hls.js';

export default {
  props: {
    _ucid: {
      type: String,
      default: '-1'
    }
  },
  name: "CallRoom",
  dicts: ['call_room_status'],
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 聊天房间表格数据
      callRoomList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        personNum: undefined,
        createUid: undefined,
        status: undefined,
        endTime: undefined,
        callUid: undefined,
        anwserUid: undefined,
        roomNum: undefined,
        ucid: this._ucid
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    play(res) {
      const video = this.$refs.video;
      if (Hls.isSupported()) {
        const hls = new Hls();
        hls.loadSource(res);
        hls.attachMedia(video);
        hls.on(Hls.Events.MANIFEST_PARSED, () => {
          video.play();
        });
      } else if (video.canPlayType('application/vnd.apple.mpegURL')) {
        video.src = res;
        video.addEventListener('loadedmetadata', () => {
          video.play();
        });
      }
    },
    /** 查询聊天房间列表 */
    getList() {
      this.loading = true;
      listCallRoom(this.queryParams).then(response => {
        this.callRoomList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        createTime: undefined,
        createBy: undefined,
        updateTime: undefined,
        updateBy: undefined,
        personNum: undefined,
        createUid: undefined,
        status: undefined,
        endTime: undefined,
        callUid: undefined,
        anwserUid: undefined,
        roomNum: undefined,
        ucid: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加聊天房间";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getCallRoom(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改聊天房间";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateCallRoom(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addCallRoom(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除聊天房间编号为"' + ids + '"的数据项？').then(() => {
        this.loading = true;
        return delCallRoom(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/callRoom/export', {
        ...this.queryParams
      }, `callRoom_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
